import { defineStore } from 'pinia'
import { nextTick } from 'vue'
import drag from "@/tool/drag"

export const useMainStore = defineStore('main', {
  state: () => {
    return {
      safeEye: Boolean(localStorage.safeEye),
      activeAppList: [] as Array<{ packageName: string }>
    }
  },
  actions: {
    changeSafeEye(state: boolean) {
      this.safeEye = state
      this.initSafeEye()
    },
    initSafeEye(): void {
      let safeEye = document.querySelector("#safeEye")
      if (this.safeEye) {
        if (safeEye) return
        let z = document.createElement("div")
        z.style.cssText = "pointer-events:none;position:fixed;width:100vw;height:100vh;z-index:99999999999;background:rgb(231 213 192 / 20%);top:0;left:0;"
        z.id = "safeEye"
        document.body.appendChild(z)
        localStorage.setItem('safeEye', '1')
      } else {
        safeEye?.parentNode?.removeChild(safeEye)
        localStorage.setItem('safeEye', '0')
      }
    },
    startApplication(packageName: string) {
      console.log(555)
      this.activeAppList.push({ packageName })
      nextTick(() => {
        let TEMPDOM = document.getElementById(packageName + (this.activeAppList.length - 1))!
        new drag(TEMPDOM)
      })
    }
  }

})